package vista;
import java.awt.Dialog.ModalityType;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JButton;

import javax.swing.JDialog;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import util.Utilidades;

import modelo.Cliente;
import modelo.Status;
import modelo.dao.Servidor;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class DlgSeleccionarCliente extends javax.swing.JDialog {

	{
		//Set Look & Feel
		try {
			javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

	private JScrollPane jScrollPane1;
	private JButton btBuscar;
	private JButton btAceptar;
	private JButton btSalir;
	private JLabel lblCodigo;
	private JTextField txtCodigo;
	private JTable tbClientes;
	private ArrayList<Cliente> clientes;
	private AbstractTableModel tbModelClientes;
	private Cliente cliente=null;


	
	public DlgSeleccionarCliente(JDialog dialog) {
		super(dialog);
		initGUI();
	}
	
	private void initGUI() {
		try {
			{
				getContentPane().setLayout(null);
				this.setTitle("Seleccionar Cliente");
				{
					jScrollPane1 = new JScrollPane();
					getContentPane().add(jScrollPane1);
					jScrollPane1.setBounds(26, 78, 415, 147);
					{
						clientes = Servidor.clientes().consultarClientesPorStatus(Status.ACTIVO);

						tbModelClientes = new AbstractTableModel() {
							@Override
							public Object getValueAt(int fil, int col) {
								switch (col) {
								case 0: return clientes.get(fil).getCodigo();
								case 1: return clientes.get(fil).getNombre();
								}
								
								return null;
							}
							
							@Override
							public int getRowCount() {
								return clientes.size();
							}
							
							@Override
							public String getColumnName(int col) {
								switch (col) {
								case 0: return "Codigo";
								case 1: return "Nombre";
								}
								
								return null;
							}
							@Override
							public int getColumnCount() {
								return 2;
							}
						};
						tbClientes = new JTable();
						jScrollPane1.setViewportView(tbClientes);
						tbClientes.setModel(tbModelClientes);
						tbClientes.setBounds(66, 83, 352, 170);
					}
				}
				{
					btBuscar = new JButton();
					getContentPane().add(btBuscar);
					btBuscar.setIcon(new ImageIcon(getClass().getClassLoader().getResource("iconos/buscar.PNG")));
					btBuscar.setBounds(314, 22, 41, 27);
				}
				{
					txtCodigo = new JTextField();
					getContentPane().add(txtCodigo);
					txtCodigo.setBounds(148, 22, 154, 28);
				}
				{
					lblCodigo = new JLabel();
					getContentPane().add(lblCodigo);
					lblCodigo.setText("Codigo:");
					lblCodigo.setFont(new java.awt.Font("Tahoma",1,11));
					lblCodigo.setBounds(93, 29, 49, 14);
				}
				{
					btSalir = new JButton();
					getContentPane().add(btSalir);
					btSalir.setText("Salir");
					btSalir.setIcon(new ImageIcon(getClass().getClassLoader().getResource("iconos/Salir.gif")));
					btSalir.setHorizontalTextPosition(JButton.CENTER);
					btSalir.setVerticalTextPosition(JButton.BOTTOM);
					btSalir.setBounds(376, 274, 79, 40);
				}
				{
					btAceptar = new JButton();
					getContentPane().add(btAceptar);
					btAceptar.setText("Aceptar");
					btAceptar.setIcon(new ImageIcon(getClass().getClassLoader().getResource("iconos/Aceptar.png")));
					btAceptar.setHorizontalTextPosition(JButton.CENTER);
					btAceptar.setVerticalTextPosition(JButton.BOTTOM);
					btAceptar.setBounds(270, 274, 79, 40);
				}
			}
			this.setSize(488, 369);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	DlgSeleccionarCliente dscli=this;
   public Cliente mostrar(JDialog dialog) {
		
		setLocationRelativeTo(null);
		
		darFormatos();
		
		btBuscar.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				buscar();
			}
		});
		
		txtCodigo.addKeyListener(new KeyAdapter() {
				@Override
				public void keyTyped(KeyEvent ke) {
					if (ke.getKeyChar()== KeyEvent.VK_ENTER) {
						buscar();
					}
				}
		});
		
		btAceptar.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if(Utilidades.seleccion(tbClientes, dscli, "Cliente"))
				{ 	cliente=clientes.get(dscli.tbClientes.getSelectedRow());
					dscli.dispose();
				}
			}
		});
		
		btSalir.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				dscli.dispose();
			}
		});
		
		this.setModalityType(ModalityType.APPLICATION_MODAL);
		setVisible(true);
		return cliente;
		
	}
   
   public void buscar()
	{   DlgCliente dcli  = new DlgCliente(dscli);
		if(!(Utilidades.campoVacio(dscli, txtCodigo, "Codigo")))
		{	Cliente c = Servidor.clientes().consultarClientePorCodigo(txtCodigo.getText());
			if(c!=null)
			{	int i = clientes.indexOf(c);
			  	tbClientes.setRowSelectionInterval(i, i);
			}
			else
			{	int resp=JOptionPane.showConfirmDialog(dscli, "El Cliente no esta Registrado Desea Registrarlo?", "Confirmar Inclusion",JOptionPane.YES_NO_OPTION);
				if(resp==JOptionPane.YES_OPTION)
				{	dcli.mostrarIncluir(dscli,txtCodigo.getText());
					clientes = Servidor.clientes().consultarClientesPorStatus(Status.ACTIVO);
					tbModelClientes.fireTableDataChanged();
				}
			}
		}
	}
   
   public void darFormatos(){ 
		Utilidades.convertirMayuscula(txtCodigo);
	}
   
   

}
